import { storage } from "@/utils/storage";
const types = {
  // 初始化权限数据
  INIT_PERMISSION: "init_permission",
  ADD_PERMISSION: "add_permission",
};
// state
const state = {
  // 权限列表
  permission: [],
};

// getters
const getters = {
  // 过滤权限
  fliterPermission: (state, getters) => (buttons) => {
    console.log( state.permission,'ggg')
    return buttons.filter((item) => state.permission.includes(item.permission));
  },
};

// mutations
const mutations = {
  // 初始化权限数据
  [types.ADD_PERMISSION](state, permission) {
    state.permission = permission;
  },
};

// actions
const actions = {
  addPermission({ commit, state }, permission) {
    commit(types.ADD_PERMISSION, permission);
  },
  initPermission({ commit, state }) {
    let nodes = storage.local.get(storage.events.node)
    let permission = [];
    if(nodes?.length){
      permission = nodes.map((item) => item.code);
    }
    
    commit(types.ADD_PERMISSION, permission);
  },
};

const permission = {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};

export default permission;
